package birdGame;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.awt.Graphics;
import javax.swing.JPanel;
//游戏的主类
public class BirdGame extends JPanel{
    BufferedImage bg,startbg,gameoverbg;
    //定义地面对象
    Ground ground;
    //记录游戏状态
    int state=0;
    // 定义几个常量来存储游戏状态
    static final int START=0;
    static final int RUNNING=1;
    static final int GAMEOVER=2;
    Bird bird;
    int score=0;
    column column;
    public BirdGame() {
        //加载图片
        try {
            //加载背景
            bg=ImageIO.read(getClass().getResource("bg.png"));
            //实例化地面
            ground=new Ground();
            //实例化小鸟
            bird=new Bird();
            startbg=ImageIO.read(getClass().getResource("start.png"));
            column=new column();
            gameoverbg=ImageIO.read(getClass().getResource("gameover.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //把图片绘制出来
    //画笔函数
    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);
        //把刚刚加载到的图片进行了绘制
        g.drawImage(bg, 0, 0, null);
        g.drawImage(bird.image, bird.x, bird.y, null);
        switch (state) {
            case START:
                g.drawImage(startbg, 0, 0, null);
                break;
            case RUNNING:
                //柱子绘制
                g.drawImage(column.image, column.x, column.y, null);
                //字体
                Font font=new Font(Font.DIALOG, Font.BOLD, 40);
                g.setFont(font);
                //颜色
                g.setColor(Color.white);
                g.drawString(score+"", 40, 50);

                break;
            case GAMEOVER:
                g.drawImage(gameoverbg, 0, 0, null);
                break;
        }
        g.drawImage(ground.image, ground.x, ground.y, null);
    }
    //定义一个动画函数:控制整个游戏当中的各种动画
    public void action() {
        this.addMouseListener(new MyMouseListener());
        while (true) {
            switch (state) {
                case START:
                    ground.step();
                    bird.fly();
                    break;
                case RUNNING:
                    ground.step();
                    bird.fly();
                    column.step();
                    bird.down();
                    //得分判断
                    if(bird.x==column.x+column.width) {
                        score++;
                    }
                    //碰撞检测
                    if (bird.hitGround()||bird.hitsky()||bird.hitColumn(column)) {
                        state=GAMEOVER;
                    }
                    break;
                case GAMEOVER:

                    break;
                default:
                    break;
            }
            //刷新画布
            repaint();
            try {
                Thread.sleep(1000/60);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    //定义鼠标监听的类       鼠标适配器
    class MyMouseListener extends MouseAdapter{
        //重写鼠标按下方法
        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
            super.mousePressed(e);
            switch (state) {
                case START:
                    state=RUNNING;
                    break;
                case RUNNING:
                    bird.up();
                    break;
                case GAMEOVER:
                    //重新开始
                    state=START;
                    //初始化
                    score=0;
                    bird=new Bird();
                    column=new column();
                    break;
            }
        }
    }
}
